// pages/collect/collect.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    page:1,
    list:[],
    thumbUrl:getApp().globalData.thumbUrl,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var uid = wx.getStorageSync('uid')
    var param = {
      uid: uid,
      page:1
    };
    app.wxRequest('POST', "Index/collGoodsList", param, (res) => {
      var data = res.data;
      console.log(data)
      that.setData({
        list: data
      })
    }, (err) => {
      console.log(err.errMsg)
    })
  },
  cancleTap:function(e){
    var that = this;
    var id = e.currentTarget.dataset.id;
    wx.showModal({
      title: '取消收藏',
      content: '确定要取消收藏该商品吗？',
      showCancel: true, //是否显示取消按钮
      cancelText: "取消", //默认是“取消”
      cancelColor: '#9C9C9C', //取消文字的颜色
      confirmText: "确定", //默认是“确定”
      confirmColor: '#1AA034', //确定文字的颜色
      success: function (res) {
        if (res.cancel) {
          //点击取消,默认隐藏弹框
        } else {
          var param = {
            id: id
          };
          app.wxRequest('POST', "Index/cancelColl", param, (res) => {
            var code = res.code;
            if (code == 1) {
              wx.showToast({
                title: res.msg,
                icon: "none",
                duration: 1500
              })
              that.onLoad();
            } else {
              wx.showToast({
                title:res.msg,
                icon: "none",
                duration: 1500
              })
            }
          }, (err) => {
            console.log(err.errMsg)
          })
        }
      },
      fail: function (res) {}, //接口调用失败的回调函数
      complete: function (res) {}, //接口调用结束的回调函数（调用成功、失败都会执行）
    })
    // var id=e.curr
  },
  goodsDetail:function(e){
    var type=e.currentTarget.dataset.type;
    var id=e.currentTarget.dataset.id;
    var name=e.currentTarget.dataset.name
    if(type == 1){//抢购
      wx.navigateTo({
        url: '../robGoodsDetails/robGoodsDetails?name='+name+"&id="+id,
      })
    }else if(type == 2){//拼团
      wx.navigateTo({
        url: '../shopDetails/shopDetails?name='+name+"&id="+id,
      })
    }
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },


  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.showNavigationBarLoading();
    var that=this;
    var uid=wx.getStorageSync('uid');
    var param = {
      uid: uid,
      page:1
    };
    app.wxRequest('POST', "Index/collGoodsList", param, (res) => {
      var data = res.data;
      console.log(data)
      that.setData({
        list: data
      })
    }, (err) => {
      console.log(err.errMsg)
    })
    setTimeout(function(){
      wx.hideNavigationBarLoading();
      wx.stopPullDownRefresh();
    },3000)
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that=this;
    var page=that.data.page;
    var list=that.data.list;
    var newPage=parseInt(page) + 1;
    var uid=wx.getStorageSync('uid');
    var param = {
      uid: uid,
      page:newPage
    };
    app.wxRequest('POST', "Index/collGoodsList", param, (res) => {
      var data = res.data;
      var code=res.code;
      if(code == 1){
        that.setData({
          list: list.concat(data),
          page:newPage
        })
      }else{
        wx.showToast({
          title: res.msg,
          icon:"none",
          duration:1500
        })
      }
      
    }, (err) => {
      console.log(err.errMsg)
    })
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})